<?php

namespace app\admin\controller\census;

use app\common\controller\Backend;
use think\Session;
/**
 * 店铺管理
 *
 * @icon fa fa-circle-o
 */
class Reconciliation extends Backend
{

    /**
     * Reconciliation模型对象
     * @var \app\admin\model\census\Reconciliation
     */
    protected $model = null;
    //卡项模型
    protected $goods_order_model = null;
    protected $order_item_model = null;
    protected $count_order_model = null;
    protected $shop_id = null;
    public function _initialize()
    {   
        parent::_initialize();
        $this->shop_id = Session::get('shop');
        $this->model = new \app\admin\model\census\Reconciliation;
        $this->goods_order_model = new \app\admin\model\order\Goods;
        $this->order_item_model = new \app\admin\model\order\Item;
        $this->count_order_model = new \app\admin\model\census\Adate;
        $this->view->assign("stateList", $this->model->getStateList());
        $this->view->assign("verifyList", $this->model->getVerifyList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $conditions = '1=1';
            if ($this->shop_id != 1) {
                $conditions = ['reconciliation.id' => $this->shop_id];
            }
            
            $list = $this->model->alias('s')
            ->where($where)
            ->where($conditions)
            ->with(['order'])
            ->order($sort, $order)
            ->paginate($limit);

            if ($list->total() > 0) {
                $list_data = $list->items();
                foreach ($list_data as $key => &$value) {
                    $conditions = [];
                    $conditions['go.gdtype'] = "0";//0为商品，1为服务
                    $conditions['go.shop_id'] = $value['id'];
                    $conditions['o.state'] = 6;
                    $conditions['o.data_type'] = ['in',['cards','goods']];
                    $conditions['o.createtime'] = [['>=',$value['order']['adatetime']],['<=',$value['order']['adatetime'] + 3600 * 24 - 1]];

                    $value['count_goods_order'] = 0;
                    $value['count_services_order'] = 0;
                    $value['count_card_order'] = 0;
                    $value['count_refund_order'] = 0;
                    //商品营业额
                    $value['count_goods_order'] = $this->goods_order_model->alias('go')
                                                ->join('wanlshop_order o','o.id = go.order_id','left')
                                                ->where($conditions)
                                                ->sum('go.actual_payment');
                    
                    //服务营业额
                    $conditions['go.gdtype'] = "1";//0为商品，1为服务
                    $value['count_services_order'] = $this->goods_order_model->alias('go')
                                                ->join('wanlshop_order o','o.id = go.order_id','left')
                                                ->where($conditions)
                                                ->sum('go.actual_payment');
                    //卡项营业额
                    unset($conditions['go.gdtype']);
                    $value['count_card_order'] = $this->order_item_model->alias('go')
                                                ->join('wanlshop_order o','o.id = go.order_id','left')
                                                    ->where($conditions)
                                                    ->sum('go.actual_price');
                    $value['count_total_order'] = number_format($value['count_goods_order'] + $value['count_services_order'] + $value['count_card_order'] - $value['count_refund_order'],2);

                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
